subroutine def_p(Pobs,x,xCell,s,n,p,rho,u,c,Pinf,dpdt,drdt,dudt,ttime,dtout,dt)
    implicit none

    real *8 x(3),xcell(3),s,n(3),c,Pinf,P,Rho,u(3),dPdt, drdt,dudt(3),ttime,dtout,dt
    real(8),intent(inout) :: Pobs(*)
    
    real *8 pi,Tobs,Tobs0,p1,p2,p3,p4,p5,xdiff,ydiff,zdiff,rdiff,Rhoinf
    integer :: m
    
    Rhoinf=1.4*Pinf/c**2
    pi=3.141592653589793
    
    
    xdiff=x(1)-xCell(1)
    ydiff=x(2)-xCell(2)
    zdiff=x(3)-xCell(3)
    rdiff=sqrt(xdiff**2+ydiff**2+zdiff**2)
      
        
    p1=s*(drdt*(u(1)*n(1)+u(2)*n(2)+u(3)*n(3))+Rho*(dudt(1)*n(1)+dudt(2)*n(2)+dudt(3)*n(3)))/(4*pi*rdiff)
    p2=s*dPdt*(xdiff*n(1)+ydiff*n(2)+zdiff*n(3))/(4*pi*c*rdiff*rdiff)
    p3=s*(drdt*(u(1)*n(1)+u(2)*n(2)+u(3)*n(3))* (xdiff*u(1)+ydiff*u(2)+zdiff*u(3))+&
                Rho*(dudt(1)*n(1)+dudt(2)*n(2)+dudt(3)*n(3))*(xdiff*u(1)+ydiff*u(2)+zdiff*u(3))+&
                Rho*(u(1)*n(1)+u(2)*n(2)+u(3)*n(3))*(xdiff*dudt(1)+ydiff*dudt(2)+zdiff*dudt(3)))/(4*pi*c*rdiff*rdiff)  
    p4=s*(P-Pinf)*(xdiff*n(1)+ydiff*n(2)+zdiff*n(3))/(4*pi*rdiff*rdiff*rdiff)
    p5=s*Rho*(u(1)*n(1)+u(2)*n(2)+u(3)*n(3))*(xdiff*u(1)+ydiff*u(2)+zdiff*u(3))/(4*pi*rdiff*rdiff*rdiff)
            
    Tobs=ttime+rdiff/c
    m=int(Tobs/dtOut)+1
! 1 -[0,dtout]
! 2 -[dtout,2*dtout]
    Tobs0=(m-1)*dtout
    
    if (Tobs-dt.lt.Tobs0) then
        Pobs(m-1)=Pobs(m-1)+(p1+p2+p3+p4+p5)*(Tobs0-(Tobs-dt))/dtout
        Pobs(m)=Pobs(m)+(p1+p2+p3+p4+p5)*(Tobs-Tobs0)/dtout
        
!        write(*,*) ' if (Tobs-dt.lt.Tobs0) then!'
!        write(*,*) ' Tobs0 = ',Tobs0, ' dt=', dt, ' Tobs=', Tobs
!        write(*,*) ' m=', m, 'Pobs(m-1) = ',Pobs(m-1), ' Pobs(m)=', Pobs(m)
!        write(*,*) ' (Tobs-Tobs0)/dtout=', (Tobs-Tobs0)/dtout
    else
        Pobs(m)=Pobs(m)+(p1+p2+p3+p4+p5)*dt/dtout
    end if
        
    return
end subroutine def_p
